#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from UserDict import DictMixin


class BundleDict(QStandardItemModel, DictMixin):
    
    SYNTAX_DIR = 'Syntaxes'
    def __init__(self, parent, path):
        QStandardItemModel.__init__(self, parent)
        root = self.invisibleRootItem()
        bdir = QDir(os.path.abspath(path))
        
        if bdir.exists():
            
            # Look for the bundles
            for tmbundle in bdir.entryList(['*.tmbundle'], QDir.Dirs):
                name = str(tmbundle).replace('.tmbundle', '')
                item = QStandardItem(name)
                root.appendRow(item)
                bdir.cd( tmbundle )
                d = QDir( bdir )
                
                if d.cd(self.SYNTAX_DIR):
                    print d.exists() and d.path()
                    for syn in d.entryList(['*.plist', '*.tmLanguage'], QDir.Files):
                        #f = QFile(d.path(), syn)
                        #print f.exists()
                        
                        name = str(syn).split('.')[0]
                        syn_item = QStandardItem(name)
                        item.appendRow(syn_item)
                        
                bdir.cdUp()
        else:
            pass
        
    
    
        
    
    def empty(self):
        '''
        Is there any bundle?
        '''
        return len(self) == 0
    
    
    #def columnCount(self, index):
        #return 0
    
    #def rowCount(self, index):
        #return 0

class AddButton(QPushButton):
    '''
    Add button
    '''
    def __init__(self, parent):
        QPushButton.__init__(self, parent)
        self.setup_actions()
        self.setup_menu()
        
    def setup_actions(self):
        self.actionAddBundle = QAction("Add Bundle", self)
        self.actionAddBundle.setObjectName('actionAddBundle')
        
        self.actionAddSyntax = QAction("Add Languages", self)
        self.actionAddSyntax.setObjectName('actionAddSyntax')
        self.actionAddSyntax.setEnabled(False)
        
        
        self.actionAddSnippet = QAction("Add Snippet", self)
        self.actionAddSnippet.setObjectName('actionAddSnippet')
        self.actionAddSnippet.setEnabled(False)
    
        self.actionAddTemplate = QAction("Add Template", self)
        self.actionAddTemplate.setObjectName('actionAddTemplate')
        self.actionAddTemplate.setEnabled(False)
        
        self.actionAddMacro = QAction("Add Macro", self)
        self.actionAddMacro.setObjectName('actionAddMacro')
        self.actionAddMacro.setEnabled(False)
        
        self.actionAddDragCommands = QAction("Add Drag Command", self)
        self.actionAddDragCommands.setObjectName('actionAddDragCommands')
        self.actionAddDragCommands.setEnabled(False)
    
        self.actionAddPreferences = QAction("Add Preferences", self)
        self.actionAddPreferences.setObjectName('actionAddPreferences')
        self.actionAddPreferences.setEnabled(False)
        
    def setup_menu(self):
        menu = QMenu(self)
        menu.addAction(self.actionAddBundle)
        menu.addAction(self.actionAddSyntax)
        menu.addAction(self.actionAddSnippet)
        menu.addAction(self.actionAddTemplate)
        menu.addAction(self.actionAddMacro)
        menu.addAction(self.actionAddDragCommands)
        menu.addAction(self.actionAddPreferences)
        
        self.setMenu(menu)
    
    
# We have to have had defined AddButton berfore this import
from gui.bundleview_ui import Ui_BundleView

class BundleEditor(QWidget, Ui_BundleView):
    def __init__(self, bundles):
        QWidget.__init__(self)
        self.setupUi(self)
        self.treeViewBundles.setModel(bundles)
        
        #self.model = bundles
        QMetaObject.connectSlotsByName(self)
    
    
    @pyqtSignature('')
    def on_buttonClose_pressed(self):
        self.close()
    
    #@pyqtSignature('')
    def on_buttonRemove_pressed(self):
        print "Remove", self.treeViewBundles.selectedIndexes()
        
